import {
    GETDATA,
    A
} from '../utils/R'

function getHttpDataDispose(arr, brr, crr) {
    arr.map((v, i) => { //202003 .... 201903...201803..
        brr.map((dv, di) => {
            if (dv.rxny * 1 == v * 1) {
                crr[i] = brr[di].sums
                return
            }
        })
    })
}

export default {
    // 命名空间 (必填)
    namespace: 'leftTopPage',
    // 数据
    state: {},
    // 路由监听 执行异步操作
    subscriptions: {
        setup({
            dispatch,
            history
        }) {
            history.listen((location, action) => {
                // 参数可以直接简写成{pathname}
                if (location.pathname === '/one') {
                    dispatch({
                        type: 'GetData'
                    });
                } 
            });
        },
    },
    //异步操作
    effects: {
        * GetData({
            payload
        }, {
            call,
            put
        }) {
            const {
                data
            } = yield call(GETDATA, A);

            if (data) {
                let Year = new Date().getFullYear(),
                    Chun = [Year - 1 + '03', Year - 2 + '03', Year - 3 + '03'],
                    Qiu = [Year - 1 + '09', Year - 2 + '09', Year - 3 + '09'],
                    dispose = {
                        dataChun: [0, 0, 0, 0, 0, 0], //只存放春季的学生数据
                        dataQiu: [0, 0, 0, 0, 0, 0], //只存放秋季的学生数据
                        list: [Year - 1 + '秋', Year - 1 + '春', Year - 2 + '秋', Year - 2 + '春', Year - 3 + '秋', Year - 3 + '春'] //数据列表
                    }

                getHttpDataDispose(Chun, data, dispose.dataChun)
                getHttpDataDispose(Qiu, data, dispose.dataQiu)
                // data处理完毕
                // 如果请求成功 data 就会有数据
                yield put({
                    type: 'setData',
                    payload: dispose,
                });
            }
        },
    },
    // 同步操作
    reducers: {
        setData(state, action) {
            return action.payload
        },
    },
};